package net.borderwars.logging;

import java.util.logging.*;

/**
 * @author ehubbard
 *         Date: Jun 28, 2004
 *         Time: 12:29:52 PM
 */
public class StreamLogger {
    static Formatter of = new OurFormatter ();

    public static class OurFormatter extends Formatter {

        /**
         * Format the given log record and return the formatted string.
         * <p/>
         * The resulting formatted String will normally include a
         * localized and formated version of the LogRecord's message field.
         * The Formatter.formatMessage convenience method can (optionally)
         * be used to localize and format the message field.
         *
         * @param record the log record to be formatted.
         * @return the formatted log record
         */
        public String format (LogRecord record) {
            StringBuffer sb = new StringBuffer (1000);
            Object parms[] = record.getParameters ();
            if (parms != null) {
                for (int i = 0; i < parms.length; i++) {
                    Object o = parms[i];
                    sb.append (o.toString ());
                    sb.append (" ");
                }
            }

            return ("blah");
        }
    }

    static public class OurLogger extends Logger {
        public OurLogger (String name, String resourceBundleName) {
            super (name, resourceBundleName);
        }

        public OurLogger () {
            super ("borderwars.err", null);
            this.addHandler (new StreamHandler (System.err, of));
            this.setFilter (null);
        }
    }

    public static LogManager getLogManager () {
        LogManager lm = LogManager.getLogManager ();
        return (lm);

    }

    public static Logger getLogger () {

        Logger l = new OurLogger ();
        return (l);
    }

    private static void removeHandlers (Logger l) {
        Handler h[] = l.getHandlers ();
        for (Handler handler : h) {
            l.removeHandler (handler);
        }
    }

    public static Logger getErrLogger () {
        Logger l = new OurLogger ();
        removeHandlers (l);
        StreamHandler sh = new StreamHandler (System.err, of);
        l.addHandler (sh);
        return (l);
    }

    public static Logger getOutLogger () {
        Logger l = new OurLogger ();
        removeHandlers (l);
        StreamHandler sh = new StreamHandler (System.out, of);
        l.addHandler (sh);
        return (l);
    }

}
